package org.dromara.xyyw.controller;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xyyw.domain.bo.XyywGoodsBo;
import org.dromara.xyyw.domain.query.XyywGoodsQuery;
import org.dromara.xyyw.domain.vo.XyywGoodsVo;
import org.dromara.xyyw.service.IXyywGoodsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商品
 *
 * @author yf
 * @date 2024-05-29
 */
@Validated
@RestController
@RequestMapping("/xyyw/goods")
public class XyywGoodsController extends BaseController {

    @Autowired
    private IXyywGoodsService xyywGoodsService;

    /**
     * 查询商品列表
     */
    @SaCheckPermission("xyyw:goods:list")
    @GetMapping("/list")
    public TableDataInfo<XyywGoodsVo> list(XyywGoodsQuery query) {
        return xyywGoodsService.queryPageList(query);
    }

    /**
     * 导出商品列表
     */
    @SaCheckPermission("xyyw:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XyywGoodsQuery query, HttpServletResponse response) {
        List<XyywGoodsVo> list = xyywGoodsService.queryList(query);
        ExcelUtil.exportExcel(list, "商品", XyywGoodsVo.class, response);
    }

    /**
     * 获取商品详细信息
     *
     * @param goodId 主键
     */
    @SaCheckPermission(value = {"xyyw:goods:query", "xyyw:goods:edit"}, mode = SaMode.OR)
    @GetMapping("/{goodId}")
    public R<XyywGoodsVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer goodId) {
        return R.ok(xyywGoodsService.queryById(goodId));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("xyyw:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XyywGoodsBo bo) {
        return toAjax(xyywGoodsService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("xyyw:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XyywGoodsBo bo) {
        return toAjax(xyywGoodsService.updateByBo(bo));
    }

    /**
     * 删除商品
     *
     * @param goodIds 主键串
     */
    @SaCheckPermission("xyyw:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] goodIds) {
        return toAjax(xyywGoodsService.deleteWithValidByIds(List.of(goodIds)));
    }
}
